package cn.com.ghy.android.widget.coopvisit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.com.ghy.android.bean.CoopVisitBigShopInfoBean;
import cn.com.ghy.android.bean.CoopVisitInfoBean;
import cn.com.ghy.android.bean.CoopVisitOptionInfoBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.VsPartOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天协访大店信息显示
 * @author wuz  @date 2011-6-22 08:38:55
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class CoopVisitBigShop extends TabActivity {

	private TextView labTxtBigShopId = null;
	private TextView labTxtBigShopName = null;
	private TextView labTxtLeavetime = null;
	private TextView labTxtVisitMin = null;
	private TextView labTxtEntertime = null;

	private EditText txtContent = null;
	private EditText txtAvgmonthsale = null;
	private EditText txtPremonthsale = null;
	private EditText txtFirstcmptsale = null;

	private CheckBox chkPjpvisit = null;
	private CheckBox chkPrepare = null;
	private CheckBox chkShelfall = null;
	private CheckBox chkShelfsub = null;
	private CheckBox chkPricepromgood = null;
	private CheckBox chkOtherpromgood = null;
	private CheckBox chkTwoposdo = null;
	private CheckBox chkCrc = null;
	private CheckBox chkOrderform = null;

	private Spinner spiTalklevel = null;
	private Spinner spiHandlevel = null;
	private Spinner spiShelflevel = null;
	private Spinner spiCmulevel = null;
	private Spinner spiTwoposall = null;
	private Spinner spiPosm = null;
	private Spinner spiKeep = null;

	private Button btnSubmit01 = null;
	private Button btnSubmit02 = null;
	private Button btnSubmit03 = null;
	private Button btnSubmit04 = null;

	private ProgressDialog progressDialog = null;

	private Bundle bundle = null;
	private LoginInfoBean loginInfo = null;
	private CoopVisitInfoBean coopVisitInfo = null;
	private List<CoopVisitBigShopInfoBean> coopVisitBigShopInfoList = null;
	private CoopVisitBigShopInfoBean coopVisitBigShopInfo = null;
	private List<CoopVisitOptionInfoBean> option = null;
	private ArrayAdapter<CoopVisitOptionInfoBean> spiAdapter = null;
	private static final String VSTYPE = CommonStatic.VSTYPE_BIG;

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.coop_visit_big_shop);
		getSession();
		TabHost tabHost = getTabHost();

		TabSpec tab1 = tabHost.newTabSpec("tab1");
		tab1.setIndicator("基本信息");
		tab1.setContent(R.id.linBaseInfo);
		tabHost.addTab(tab1);

		TabSpec tab2 = tabHost.newTabSpec("tab2");
		tab2.setIndicator("销量和基本功");
		tab2.setContent(R.id.linSale);
		tabHost.addTab(tab2);

		TabSpec tab3 = tabHost.newTabSpec("tab3");
		tab3.setIndicator("现状");
		tab3.setContent(R.id.linStatus);
		tabHost.addTab(tab3);

		TabSpec tab4 = tabHost.newTabSpec("tab4");
		tab4.setIndicator("表单");
		tab4.setContent(R.id.linTable);
		tabHost.addTab(tab4);

		tabHost.setCurrentTab(0);

		TabWidget tabWidget = tabHost.getTabWidget();
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			tabWidget.getChildAt(i).getLayoutParams().height = getWindowManager()
					.getDefaultDisplay().getHeight() / 12;
		}
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) getApplication()).getLoginInfo();
		bundle = getIntent().getExtras();
		coopVisitInfo = (CoopVisitInfoBean) bundle.getSerializable("coopVisitInfo");
		coopVisitBigShopInfoList = (List<CoopVisitBigShopInfoBean>) bundle
				.getSerializable("coopVisitBigShopInfoList");
		option = new ArrayList<CoopVisitOptionInfoBean>();
		for (int i = 0; i < 6; i++) {
			if (i == 0) {
				option.add(new CoopVisitOptionInfoBean("", "请选择..."));
			} else {
				option.add(new CoopVisitOptionInfoBean("" + i, "" + i));
			}
		}

		spiAdapter = new ArrayAdapter<CoopVisitOptionInfoBean>(CoopVisitBigShop.this,
				android.R.layout.simple_spinner_item, option);
		spiAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	}

	private void findView() {
		labTxtBigShopId = (TextView) findViewById(R.id.labTxtBigShopId);
		labTxtBigShopName = (TextView) findViewById(R.id.labTxtBigShopName);
		labTxtLeavetime = (TextView) findViewById(R.id.labTxtLeavetime);
		labTxtVisitMin = (TextView) findViewById(R.id.labTxtVisitMin);
		labTxtEntertime = (TextView) findViewById(R.id.labTxtEntertime);

		txtContent = (EditText) findViewById(R.id.txtContent);
		txtAvgmonthsale = (EditText) findViewById(R.id.txtAvgmonthsale);
		txtPremonthsale = (EditText) findViewById(R.id.txtPremonthsale);
		txtFirstcmptsale = (EditText) findViewById(R.id.txtFirstcmptsale);

		chkPjpvisit = (CheckBox) findViewById(R.id.chkPjpvisit);
		chkPrepare = (CheckBox) findViewById(R.id.chkPrepare);
		chkShelfall = (CheckBox) findViewById(R.id.chkShelfall);
		chkShelfsub = (CheckBox) findViewById(R.id.chkShelfsub);
		chkPricepromgood = (CheckBox) findViewById(R.id.chkPricepromgood);
		chkOtherpromgood = (CheckBox) findViewById(R.id.chkOtherpromgood);
		chkTwoposdo = (CheckBox) findViewById(R.id.chkTwoposdo);
		chkCrc = (CheckBox) findViewById(R.id.chkCrc);
		chkOrderform = (CheckBox) findViewById(R.id.chkOrderform);

		spiTalklevel = (Spinner) findViewById(R.id.spiTalklevel);
		spiHandlevel = (Spinner) findViewById(R.id.spiHandlevel);
		spiShelflevel = (Spinner) findViewById(R.id.spiShelflevel);
		spiCmulevel = (Spinner) findViewById(R.id.spiCmulevel);
		spiTwoposall = (Spinner) findViewById(R.id.spiTwoposall);
		spiPosm = (Spinner) findViewById(R.id.spiPosm);
		spiKeep = (Spinner) findViewById(R.id.spiKeep);

		btnSubmit01 = (Button) findViewById(R.id.btnSubmit01);
		btnSubmit02 = (Button) findViewById(R.id.btnSubmit02);
		btnSubmit03 = (Button) findViewById(R.id.btnSubmit03);
		btnSubmit04 = (Button) findViewById(R.id.btnSubmit04);
	}

	private void init() {
		labTxtBigShopId.setText(coopVisitInfo.getKunnr());
		labTxtBigShopName.setText(coopVisitInfo.getKunnrname());

		spiTalklevel.setAdapter(spiAdapter);
		spiHandlevel.setAdapter(spiAdapter);
		spiShelflevel.setAdapter(spiAdapter);
		spiCmulevel.setAdapter(spiAdapter);
		spiTwoposall.setAdapter(spiAdapter);
		spiPosm.setAdapter(spiAdapter);
		spiKeep.setAdapter(spiAdapter);

		if (null != coopVisitBigShopInfoList && coopVisitBigShopInfoList.size() > 0) {
			coopVisitBigShopInfo = coopVisitBigShopInfoList.get(0);
			txtContent.setText(null == coopVisitBigShopInfo.getContent() ? ""
					: coopVisitBigShopInfo.getContent());
			txtAvgmonthsale.setText(null == coopVisitBigShopInfo.getAvgmonthsale() ? ""
					: coopVisitBigShopInfo.getAvgmonthsale());
			txtPremonthsale.setText(null == coopVisitBigShopInfo.getPremonthsale() ? ""
					: coopVisitBigShopInfo.getPremonthsale());
			txtFirstcmptsale.setText(null == coopVisitBigShopInfo.getFirstcmptsale() ? ""
					: coopVisitBigShopInfo.getFirstcmptsale());
			if (null != coopVisitBigShopInfo.getPjpvisit()
					&& "是".equals(coopVisitBigShopInfo.getPjpvisit())) {
				chkPjpvisit.setChecked(true);
			}
			labTxtEntertime.setText(null == coopVisitBigShopInfo.getEntertime() ? ""
					: coopVisitBigShopInfo.getEntertime().substring(11, 19));
			if (null != coopVisitBigShopInfo.getPrepare()
					&& "是".equals(coopVisitBigShopInfo.getPrepare())) {
				chkPrepare.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getTalklevel()) {
				spiTalklevel.setSelection(
						getSpinnerSelectedItemPosition(coopVisitBigShopInfo.getTalklevel()), true);
			}
			if (null != coopVisitBigShopInfo.getHandlevel()) {
				spiHandlevel.setSelection(
						getSpinnerSelectedItemPosition(coopVisitBigShopInfo.getHandlevel()), true);
			}
			if (null != coopVisitBigShopInfo.getShelfall()
					&& "是".equals(coopVisitBigShopInfo.getShelfall())) {
				chkShelfall.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getShelfsub()
					&& "是".equals(coopVisitBigShopInfo.getShelfsub())) {
				chkShelfsub.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getShelflevel()) {
				spiShelflevel.setSelection(
						getSpinnerSelectedItemPosition(coopVisitBigShopInfo.getShelflevel()), true);
			}
			if (null != coopVisitBigShopInfo.getCmulevel()) {
				spiCmulevel.setSelection(
						getSpinnerSelectedItemPosition(coopVisitBigShopInfo.getCmulevel()), true);
			}
			if (null != coopVisitBigShopInfo.getPricepromgood()
					&& "是".equals(coopVisitBigShopInfo.getPricepromgood())) {
				chkPricepromgood.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getOtherpromgood()
					&& "是".equals(coopVisitBigShopInfo.getOtherpromgood())) {
				chkOtherpromgood.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getTwoposdo()
					&& "是".equals(coopVisitBigShopInfo.getTwoposdo())) {
				chkTwoposdo.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getTwoposall()) {
				spiTwoposall.setSelection(
						getSpinnerSelectedItemPosition(coopVisitBigShopInfo.getTwoposall()), true);
			}
			if (null != coopVisitBigShopInfo.getPosm()) {
				spiPosm.setSelection(
						getSpinnerSelectedItemPosition(coopVisitBigShopInfo.getPosm()), true);
			}
			if (null != coopVisitBigShopInfo.getCrc() && "是".equals(coopVisitBigShopInfo.getCrc())) {
				chkCrc.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getOrderform()
					&& "是".equals(coopVisitBigShopInfo.getOrderform())) {
				chkOrderform.setChecked(true);
			}
			if (null != coopVisitBigShopInfo.getKeep()) {
				spiKeep.setSelection(
						getSpinnerSelectedItemPosition(coopVisitBigShopInfo.getKeep()), true);
			}
			if (null != coopVisitBigShopInfo.getLeavetime()
					&& !"".equals(coopVisitBigShopInfo.getLeavetime())) {
				labTxtLeavetime.setText(coopVisitBigShopInfo.getLeavetime().substring(11, 19));
				Date dateEn = null;
				Date dateLe = null;
				SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
				try {
					dateEn = format.parse(coopVisitBigShopInfo.getEntertime().substring(11, 19));
					dateLe = format.parse(coopVisitBigShopInfo.getLeavetime().substring(11, 19));
					int visitMin = (int) (dateLe.getTime() - dateEn.getTime()) / 1000 / 60;
					labTxtVisitMin.setText(visitMin + "");
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		} else {
			coopVisitBigShopInfo = new CoopVisitBigShopInfoBean();
		}
	}

	private void addListener() {
		spiTalklevel.setOnItemSelectedListener(new SpiListener("Talklevel"));
		spiHandlevel.setOnItemSelectedListener(new SpiListener("Handlevel"));
		spiShelflevel.setOnItemSelectedListener(new SpiListener("Shelflevel"));
		spiCmulevel.setOnItemSelectedListener(new SpiListener("Cmulevel"));
		spiTwoposall.setOnItemSelectedListener(new SpiListener("Twoposall"));
		spiPosm.setOnItemSelectedListener(new SpiListener("Posm"));
		spiKeep.setOnItemSelectedListener(new SpiListener("Keep"));

		btnSubmit01.setOnClickListener(btnListener);
		btnSubmit02.setOnClickListener(btnListener);
		btnSubmit03.setOnClickListener(btnListener);
		btnSubmit04.setOnClickListener(btnListener);
	}

	private void getData() {
		if (null == coopVisitBigShopInfo.getPartner()) {
			coopVisitBigShopInfo.setPartner(labTxtBigShopId.getText().toString().trim());
		}
		coopVisitBigShopInfo.setVsdate(loginInfo.getNowdate().substring(0, 10));
		coopVisitBigShopInfo.setCuser(loginInfo.getPernr());
		coopVisitBigShopInfo.setContent(txtContent.getText().toString().trim());
		coopVisitBigShopInfo.setAvgmonthsale(txtAvgmonthsale.getText().toString().trim());
		coopVisitBigShopInfo.setPremonthsale(txtPremonthsale.getText().toString().trim());
		coopVisitBigShopInfo.setFirstcmptsale(txtFirstcmptsale.getText().toString().trim());
		if (chkPjpvisit.isChecked()) {
			coopVisitBigShopInfo.setPjpvisit("是");
		} else {
			coopVisitBigShopInfo.setPjpvisit("否");
		}
		if (chkPrepare.isChecked()) {
			coopVisitBigShopInfo.setPrepare("是");
		} else {
			coopVisitBigShopInfo.setPrepare("否");
		}
		if (chkShelfall.isChecked()) {
			coopVisitBigShopInfo.setShelfall("是");
		} else {
			coopVisitBigShopInfo.setShelfall("否");
		}
		if (chkShelfsub.isChecked()) {
			coopVisitBigShopInfo.setShelfsub("是");
		} else {
			coopVisitBigShopInfo.setShelfsub("否");
		}
		if (chkPricepromgood.isChecked()) {
			coopVisitBigShopInfo.setPricepromgood("是");
		} else {
			coopVisitBigShopInfo.setPricepromgood("否");
		}
		if (chkOtherpromgood.isChecked()) {
			coopVisitBigShopInfo.setOtherpromgood("是");
		} else {
			coopVisitBigShopInfo.setOtherpromgood("否");
		}
		if (chkTwoposdo.isChecked()) {
			coopVisitBigShopInfo.setTwoposdo("是");
		} else {
			coopVisitBigShopInfo.setTwoposdo("否");
		}
		if (chkCrc.isChecked()) {
			coopVisitBigShopInfo.setCrc("是");
		} else {
			coopVisitBigShopInfo.setCrc("否");
		}
		if (chkOrderform.isChecked()) {
			coopVisitBigShopInfo.setOrderform("是");
		} else {
			coopVisitBigShopInfo.setOrderform("否");
		}
	}

	private class SpiListener implements OnItemSelectedListener {

		private String flag = "";

		public SpiListener(String flag) {
			this.flag = flag;
		}

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			CoopVisitOptionInfoBean bean = (CoopVisitOptionInfoBean) parent
					.getItemAtPosition(position);
			if ("Talklevel".equals(flag)) {
				coopVisitBigShopInfo.setTalklevel(bean.getOptionId());
			} else if ("Handlevel".equals(flag)) {
				coopVisitBigShopInfo.setHandlevel(bean.getOptionId());
			} else if ("Shelflevel".equals(flag)) {
				coopVisitBigShopInfo.setShelflevel(bean.getOptionId());
			} else if ("Cmulevel".equals(flag)) {
				coopVisitBigShopInfo.setCmulevel(bean.getOptionId());
			} else if ("Twoposall".equals(flag)) {
				coopVisitBigShopInfo.setTwoposall(bean.getOptionId());
			} else if ("Posm".equals(flag)) {
				coopVisitBigShopInfo.setPosm(bean.getOptionId());
			} else if ("Keep".equals(flag)) {
				coopVisitBigShopInfo.setKeep(bean.getOptionId());
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	}

	private View.OnClickListener btnListener = new View.OnClickListener() {

		@Override
		public void onClick(View paramView) {
			getData();
			httpThread = new HttpThread(handler);
			httpThread.doStart();
		}
	};

	private class HttpThread extends Thread {
		private Handler handle = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart() {
			progressDialog = new ProgressDialog(CoopVisitBigShop.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = handle.obtainMessage();
			;
			bundle = new Bundle();
			String result = null;
			VsPartOpeService service = new VsPartOpeService();
			try {
				result = service.SendVsPartData(VSTYPE, coopVisitBigShopInfo);
				progressDialog.dismiss();
				bundle.putString("result", result);
				message.what = CommonStatic.STATE_NORMAL;
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
			message.setData(bundle);
			handle.sendMessage(message);
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			String result = m.getData().getString("result");
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					if ("OK".equals(result)) {
						new AlertDialog.Builder(CoopVisitBigShop.this).setTitle("提示信息")
								.setMessage("提交成功, 是否关闭此页面!")
								.setPositiveButton("确定", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										CoopVisitBigShop.this.finish();
									}
								}).setNeutralButton("取消", null).show();
					} else {
						new AlertDialog.Builder(CoopVisitBigShop.this).setTitle("提示信息")
								.setMessage(result).setNeutralButton("关闭", null).show();
					}
					break;
			}
		}
	};

	private int getSpinnerSelectedItemPosition(String value) {
		int position = 0;
		for (int i = 0; i < option.size(); i++) {
			if (value.equals(option.get(i).getOptionId())) {
				position = i;
			}
		}
		return position;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(CoopVisitBigShop.this).setTitle("提示信息").setMessage("确定退出?")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dlg, int sumthin) {
							CoopVisitBigShop.this.finish();
						}
					}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dlg, int sumthin) {
						}
					}).show();
		}
		return true;
	}
}
